package com.lwm.string;

import com.sun.org.apache.xpath.internal.operations.String;
import org.junit.Test;

import java.util.Date;

/**
 * @author liweimin
 * @date 2020/10/15 13:50
 * @description
 */
public class StringBufferBuilder {
    /**
     * String 不可变的字符序列 使用char存储
     * StringBuffer 可变的字符序列 线程安全（效率低） 使用char存储
     * StringBuilder 可变的字符序列（jdk1.5） 线程不安全（效率高） 使用char存储
     */
    public static void main(String[] args) {
        /**
         * 默认情况扩容为2倍+2------------》使用StringBuffer(int a);--------------->指定容量
         */
        StringBuffer sb = new StringBuffer("abc");
        sb.setCharAt(0, '(');
        System.out.println(sb);
        sb.append(132);
        System.out.println(sb);
        System.out.println(sb.delete(3, 6));
        System.out.println(sb.insert(2, "fsd"));
        System.out.println(sb.reverse());
    }

    @Test
    public void test() {
        StringBuffer stringBuffer = new StringBuffer("");
        StringBuilder stringBuilder = new StringBuilder("");
        java.lang.String string = "";
        long start = System.currentTimeMillis();
        for (int i = 0; i < 2_000_00; i++) {
            stringBuffer.append(java.lang.String.valueOf(i));
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuffer耗时为" + (end - start));


        long start1 = System.currentTimeMillis();
        for (int i = 0; i < 2_000_00; i++) {
            stringBuilder.append(java.lang.String.valueOf(i));
        }
        long end1 = System.currentTimeMillis();
        System.out.println("StringBuilder耗时为" + (end1 - start1));


        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 2_000_00; i++) {
            string = string + i;
        }
        long end2 = System.currentTimeMillis();
        System.out.println("String耗时为" + (end2 - start2));
    }

    @Test
    public void test02() {
        Date date = new Date();
        java.sql.Date date1 = new java.sql.Date(date.getTime());
    }
}
